﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Layers;
using System.Text;
using System.ComponentModel;

namespace HoldemGame
{
    public class SeatEventArgs : EventArgs
    {
        public enum EventKind
        {
            SitDown,
            StandUp,
            TableResize
        }
        public EventKind Kind;
        public GameSeat Seat;
        public GamePlayer Player;

        public SeatEventArgs(EventKind kind, GameSeat seat, GamePlayer p)
        {
            Kind=kind;
            Seat=seat;
            Player=p;
        }
    }

    public class GameSeat : GameObject
    {
        private GamePlayer _player;
        private GameSeats _seats;

         public GameSeat(GameSeats seats)
        {
            _seats = seats;
        }

        public GamePlayer Player 
        { 
            get { return _player; }
            set { SitDown(value); }
        }

        public GameSeats Seats
        {
            get { return _seats; }
        }

        public bool IsFree { get { return _player == null; } }

        public void SitDown(GamePlayer p)
        {
            if (p.Seat != null)
                throw new InvalidOperationException("Player "+p+" is already sitting in");
            p.Seat = this;
           
            _player = p;
            if (null != _seats._seatEvents)
                _seats._seatEvents(_seats,new SeatEventArgs(SeatEventArgs.EventKind.SitDown, this, p));
        }

        public void StandUp(GamePlayer p)
        {
            if (p.Seat != this)
                throw new InvalidOperationException("Player "+p+" does not sit here");
            if (null != _seats._seatEvents)
                _seats._seatEvents(_seats, new SeatEventArgs(SeatEventArgs.EventKind.StandUp, this, p));
            this._player = null;
        }

    }

    public class GameSeats : GameList<GameSeat>
    {
        public GamePlayers Players;
        
        internal EventHandler<SeatEventArgs> _seatEvents;

        public event EventHandler<SeatEventArgs> SeatEvent
        {
            add { _seatEvents += value; }
            remove { _seatEvents -= value; }
        }

        public void Resize(int numSeats)
        {
            while (Count < numSeats)
                Add(new GameSeat(this));
            while (Count > numSeats)
                Remove(this[Count - 1]);
            if (null != _seatEvents)
                _seatEvents(this, new SeatEventArgs(SeatEventArgs.EventKind.TableResize, null, null));
        }


        public GameSeats()
        {
            
        }

    }
}